#include <pthread.h>
#include <iostream>
#include <functional>
#include <string>
using namespace std;

namespace MyThreadmodule
{
    // 无参数模式
    class Thread
    {
        enum class TSTATUS
        {
            NEW,
            RUNNING,
            STOP
        };
        using func = std::function<void(const string &name)>;
        static void *routine(void *args)
        {
            Thread *t = static_cast<Thread *>(args);
            t->_status = TSTATUS::RUNNING;
            t->_func(t->_name);
            return nullptr;
        }

    public:
        static int namenum;
        Thread(func f)
            : _func(f),
              _isjoinable(true),
              _status(TSTATUS::NEW)
        {
            _name = "thread-" + to_string(namenum++);
        }
        bool Start()
        {
            if (_status == TSTATUS::NEW)
            {
                int n = pthread_create(&_pid, nullptr, routine, this);
                if (n < 0)
                {
                    perror("创建进程失败");
                    return false;
                }
                return true;
            }
            else
            {
                cout << "pthread状态不是最新" << endl;
                return false;
            }
        }
        string& Name(){
            return _name;
        }
        bool Stop()
        {
            if (_status == TSTATUS::RUNNING)
            {
                int n = pthread_cancel(_pid);
                if (n < 0)
                {
                    perror("pthread_cancel error");
                    return false;
                }
                return true;
            }
            else
            {
                cout << "请启动线程" << endl;
                return false;
            }
        }
        bool Join()
        {
            int n = pthread_join(_pid, nullptr);
            if (n == 0)
            {
                cout << "等待线程成功" << endl;
                return true;
            }
            else
            {
                cout << "等待线程失败" << endl;
                return false;
            }
        }
        bool Detach()
        {
            int n = pthread_detach(_pid);
            if (n == 0)
            {
                cout << "分离成功" << endl;
                return true;
            }
            else
            {
                cout << "分离失败" << endl;
                return false;
            }
        }
        bool IsJoinbale() { return _isjoinable; }
        void EnableDetech() { _isjoinable = false; }

    private:
        std::string _name;
        func _func;
        pthread_t _pid;
        TSTATUS _status;
        bool _isjoinable;
    };
    int Thread::namenum = 1;
}
